package com.yuwj.chinaa.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.bumptech.glide.Glide;
import com.yuwj.chinaa.R;
import com.yuwj.chinaa.activity.TradeWithBuyDetailActivity;
import com.yuwj.chinaa.activity.TradeWithSellDetailActivity;
import com.yuwj.chinaa.base.BaseLazyLoadFragment;
import com.yuwj.chinaa.base.ConstantParam;
import com.yuwj.chinaa.entity.EntityOrderWithTrade;
import com.yuwj.chinaa.entity.EntityOrderWithTrade;
import com.yuwj.chinaa.entity.EntityUser;
import com.yuwj.chinaa.presenter.IPresenter;
import com.yuwj.chinaa.presenter.presenterImpl.PresenterImpl;
import com.yuwj.chinaa.util.SharedpreferenceUtil;
import com.yuwj.chinaa.util.ToastUtil;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import de.hdodenhof.circleimageview.CircleImageView;

public class FragmentOrderStateFinished extends BaseLazyLoadFragment {
    @BindView(R.id.srl_swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.rv_order_list)
    RecyclerView rvOrderList;
    List<EntityOrderWithTrade> orderList;
    CommonAdapter<EntityOrderWithTrade> adapterWithOrder;
    IPresenter presenter;
    EntityUser entityUser;


    @Override
    public int setCustomContentViewResourceId() {
        return R.layout.fragment_order_state_common;
    }

    public static FragmentOrderStateFinished createInstance() {
        FragmentOrderStateFinished fragmentMain = new FragmentOrderStateFinished();
        Bundle bundle = new Bundle();
        fragmentMain.setArguments(bundle);
        return fragmentMain;
    }

    @Override
    protected void lazyLoad() {
        super.lazyLoad();
        initParam();
        initView();
        initOperator();
    }


    private void initParam() {
        orderList = new ArrayList<>();
        entityUser = SharedpreferenceUtil.getInstance().getUserInfo(context);
        presenter = new PresenterImpl(this);


        adapterWithOrder = new CommonAdapter<EntityOrderWithTrade>(context, R.layout.item_order_list_with_asking_buy, orderList) {
            @Override
            protected void convert(ViewHolder holder, EntityOrderWithTrade entityOrder, int position) {
                holder.setText(R.id.tv_order_no, "订单编号：" + entityOrder.getID() + "");
                holder.setText(R.id.tv_time, entityOrder.getCreateTimeName());
                holder.setText(R.id.tv_state, entityOrder.getTransactionStatusName() + "");
                holder.setText(R.id.tv_cny, entityOrder.getPriceCountRMB() + " CNY");
                holder.setText(R.id.tv_price, "单价：" + entityOrder.getPriceRMB() + " CNY");
                holder.setText(R.id.tv_count, "数量：" + entityOrder.getChinaANumber() + " china-A");

            }
        };

    }

    private void initView() {
        rvOrderList.setAdapter(adapterWithOrder);
        rvOrderList.setLayoutManager(new LinearLayoutManager(context));
        adapterWithOrder.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                EntityOrderWithTrade order = orderList.get(position);
                //处理完毕的交易,不进行跳转
//                switch (order.getTransactionType()) {
//                    case ConstantParam.TRANSACTION_TYPE_WITH_BUY:
//                        startActivity(TradeWithBuyDetailActivity.createIntent(context, order.getID()));
//                        break;
//                    case ConstantParam.TRANSACTION_TYPE_WITH_SELL:
//                        startActivity(TradeWithSellDetailActivity.createIntent(context, order.getID()));
//                        break;
//                }
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });

    }


    private void initOperator() {
        refresh();
    }

    public  void refresh() {
        if(presenter == null){
            return;
        }
        JSONObject param = new JSONObject();
        try {
            param.put("pageindex", "1");
            param.put("pagesize", "100");
            param.put("ordertype", 0 + "");//交易订单类型：0：获取卖出订单，1：获取买入订单（交易中 订单不区分买入和卖出类型）
            param.put("orderStatus", "2");//订单状态：0：等待交易，1：交易中 2：交易完成，3：申诉订单
            param.put("isAll", "1");//是否查询所有：0：所有交易订单，1：个人交易订单

        } catch (JSONException e) {
            e.printStackTrace();
        }
        presenter.queryTransactionOrderList(param.toString());
        swipeRefreshLayout.setEnabled(false);//正在刷新时不能再下拉
    }


    @Override
    public void queryTransactionOrderListSuccess(List<EntityOrderWithTrade> orderList) {
        super.queryTransactionOrderListSuccess(orderList);
        this.orderList.clear();
        this.orderList.addAll(orderList);
        adapterWithOrder.notifyDataSetChanged();
        swipeRefreshLayout.setEnabled(true);//加载完毕之后,下拉刷新重置为可用
        swipeRefreshLayout.setRefreshing(false);//加载完成,取消刷新动画
    }

    @Override
    public void queryTransactionOrderListFail(Exception e) {
        super.queryTransactionOrderListFail(e);
        ToastUtil.getInstance().shortLength(context, e.getMessage());
        swipeRefreshLayout.setEnabled(true);//加载完毕之后,下拉刷新重置为可用
        swipeRefreshLayout.setRefreshing(false);//加载完成,取消刷新动画
    }
}
